package service

import (
	"context"
	"errors"
	"fmt"
	__ "order-srv/basic/proto"
	"order-srv/handler/dao"
	"order-srv/handler/models"
	"order-srv/pkg"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedOrderServer
}

// 收货地址添加
func (s *Server) AddressCreate(_ context.Context, in *__.AddressCreateReq) (*__.AddressCreateResp, error) {
	var address models.Address
	err := dao.AddressCreate(in, address)
	if err != nil {
		return nil, err
	}
	return &__.AddressCreateResp{
		Message: "收货地址添加成功",
	}, nil
}

// 收货地址列表
func (s *Server) AddressList(_ context.Context, in *__.AddressListReq) (*__.AddressListResp, error) {
	var address models.Address
	address.UserId = uint(in.UserId)
	list, err := dao.AddressList(in, address)
	if err != nil {
		return nil, err
	}
	return &__.AddressListResp{
		List: list,
	}, nil
}

// 收货地址删除
func (s *Server) AddressDel(_ context.Context, in *__.AddressDelReq) (*__.AddressDelResp, error) {
	err := dao.AddressDel(in)
	if err != nil {
		return nil, err
	}
	return &__.AddressDelResp{
		Id:      in.Id,
		Message: "收货地址删除成功",
	}, nil
}

// 实时快递查询
// 实时快递查询
func (s *Server) RealTimeExpress(_ context.Context, in *__.RealTimeExpressReq) (*__.RealTimeExpressResp, error) {
	di100 := pkg.KuaiDi100{}
	// 调用MapTrack函数，只传入快递公司编码和快递单号
	result, err := di100.MapTrack(in.Company, in.Tracking)
	if err != nil {
		return nil, errors.New("查询失败")
	}
	fmt.Println(result)

	return &__.RealTimeExpressResp{
		Message: result,
	}, nil
}
